package main.initiate;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

public class Configuration {
    private static String LOCATION = "Configuration.properties";
    private static Configuration singleton;
    
    public static Configuration getSingleton() {
        if(singleton != null) return singleton;
        
        InputStream input = Configuration.class.getResourceAsStream(LOCATION);
        Properties properties = new Properties();
        try {
            properties.load(input);
        } catch (IOException e) {
            System.err.println("Couldn't find configuration!");
        }
        
        singleton = new Configuration(properties);
        return singleton;
    }
    
    private String kbFile;
    private String ontologiesFile;
    private int cores;
    private String wordNetPropertyFile;
    private Map<String, OpenNLPModel> openNLPModelMap;
    
    private Configuration(Properties p) {
        this.kbFile = p.getProperty("locations.kb");
        this.ontologiesFile = p.getProperty("locations.ontologies");
        this.wordNetPropertyFile = p.getProperty("locations.wordnet.properties");
        
        openNLPModelMap = new HashMap<String, OpenNLPModel>();
        String openNLPSentenceModel = p.getProperty("locations.openNLPSentenceModel_English");
        String openNLPtokenModel = p.getProperty("locations.openNLPtokenModel_English");
        String openNLPPOSModel = p.getProperty("locations.openNLPPOSModel_English");
        OpenNLPModel model = new OpenNLPModel(openNLPSentenceModel, openNLPtokenModel, openNLPPOSModel);
        openNLPModelMap.put("english", model);
        
                
        String coreString = p.getProperty("threading.cores");
        if(coreString == null) {
            this.cores = Runtime.getRuntime().availableProcessors();
        }
        else {
            this.cores = Integer.parseInt(coreString);
            if(this.cores > Runtime.getRuntime().availableProcessors()) {
                if(!p.getProperty("threading.cores.override").equalsIgnoreCase("true")) {
                    this.cores = Runtime.getRuntime().availableProcessors();
                }
            }
        }
    }
    
    public String getKBFile() {
        return kbFile;
    }
    
    public String getOntologiesFile() {
        return ontologiesFile;
    }
    
    public int getNumberCores() {
        return this.cores;
    }

    public String getWordNetPropertyFile() {
        return wordNetPropertyFile;
    }

    public OpenNLPModel getOpenNLPModel(String language) {
        return openNLPModelMap.get(language);
    }
    
    public class OpenNLPModel {
        public String openNLPPOSModel;
        public String openNLPSentenceModel;
        public String openNLPtokenModel;

        public OpenNLPModel(String openNLPSentenceModel, String openNLPtokenModel, String openNLPPOSModel) {
            this.openNLPPOSModel = openNLPPOSModel;
            this.openNLPSentenceModel = openNLPSentenceModel;
            this.openNLPtokenModel = openNLPtokenModel;
        }        
    }
    
    
        
}
